/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef HPSCEVENTHANDLER_H
#define HPSCEVENTHANDLER_H

#include <QObject>
#include "gssglobals.h"
#include <QEvent>
#include <QTimer>


/**
	@author Archimedes Projekt <thelsb@gmail.com>
*/
class QTcpSocket;
class QDataStream;
class QMutex;

class HPSCSocketEvent : public QEvent
{
	private:
		HPSC_Command m_command;
		bool m_serialConnect;
	public:
		HPSCSocketEvent(HPSC_Command command,Type type = HPSCEvent):QEvent(type),
		m_command(command)
		{}
        HPSCSocketEvent(bool serialConnect,Type type = HPSCEventSerial):QEvent(type),
        m_serialConnect(serialConnect)
        {}
		~HPSCSocketEvent(){}
		inline HPSC_Command command() { return m_command; }
        inline bool serialConnect() { return m_serialConnect; }
};


class HPSCEventHandler : public QObject
{
	Q_OBJECT
	private:
		QDataStream *m_dataStream;
		QTcpSocket *m_socket;
		QMutex *m_socketMutex;
        QTimer *m_socketTimer; //watches the socket connetcion
        QString m_host;
        quint32 m_port;
		
		HPSCEventHandler(const HPSCEventHandler &instance):QObject(0) {}
	public:
		HPSCEventHandler(QString m_host,quint32 port);
		~HPSCEventHandler();
    QTcpSocket * socket();
    QDataStream * dataStream();
    void customEvent(QEvent *event);
    //void sendHPSCCommand(HPSC_Command cmd,MSGTYPE type);
    QMutex * socketMutex();
	signals:
		void sig_hpscCommand(HPSC_Command);
		void sig_hpscReadout(int,float,float);
        void sig_hpscReadoutA(int,float);
        void sig_hpscReadoutV(int,float);
		void sig_hpscError(int,quint8);
        void sig_hpscDisconnected();
	public slots:
    	void readSocket();
    void connectionLost();
    void sendHPSCCommand(HPSC_Command command, int type);
    
    
        
};

#endif
